package com.qf.game.servlet;

import com.qf.framwork.annotation.ResponseBody;
import com.qf.framwork.base.BaseServlet;
import com.qf.game.entity.Player;
import com.qf.game.service.PlayerService;
import com.qf.game.utils.MailUtils;
import com.qf.game.utils.ThreadUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@WebServlet(urlPatterns = "/playerServlet")
public class PlayerServlet extends BaseServlet {

    private PlayerService playerService = new PlayerService();

    /**
     * 注册方法
     */
    public String register(Player player){
        //调用service 进行用户的保存
        int result = playerService.insertPlayer(player);
        //注册成功后
        if (result > 0) {
            //跳转到登录页面
            return "redirect:login.jsp";
        } else {
            return "redirect:register.jsp?status=1";
        }
    }

    /**
     * playerServlet?method=existeUsername&username=xxxx
     * 判断账号是否存在 - ajax
     */
    @ResponseBody
    public String existeUsername(String username){
        boolean flag = playerService.existeUsername(username);
        System.out.println("判断用户名" + username + "是否存在：" + flag);
        return "{\"result\":" + flag + "}";
    }

    /**
     * 注册的servlet
     * @return
     */
    public String login(String username, String password, HttpSession session){
        Player player = playerService.queryByUsername(username, password);
        System.out.println("登录获得的结果：" + player);
        if (player != null) {
            //登录成功
            session.setAttribute("login_user", player);
            //跳转到首页
            return "redirect:index.jsp";
        }

        //未找到用户
        return "redirect:login.jsp?status=1";
    }

    /**
     * 发送修改密码的验证码的邮件
     */
    @ResponseBody
    public String sendPasswordEmail(String username, HttpSession session){
        //1、通过username 找到用户信息（邮箱）
        Player player = playerService.queryByUsername(username);
        if (player == null) {
            //账号不存在
            return "{\"status\":-1}";
        }

        //2、获取账号的绑定邮箱
        String email = player.getEmail();

        //3、给这个邮箱发送邮件 - 验证码  TODO 待会要实现邮件的发送
        int code = (int)(Math.random() * 9000) + 1000;
        session.setAttribute("code", code);
        //异步发送
        ThreadUtils.submit(() -> {
            MailUtils.sendMail(email, "找回密码", "验证码为：" + code + "，如果不是本人操作，请忽略！");
        });
        return "{\"status\":1, \"email\":\"" + email + "\"}";
    }

    /**
     * 修改密码
     * @return
     */
    public String updatePassword(String username, String newpassword, String code, HttpSession session, HttpServletRequest request){
        //1、先判断验证码是否正确
        int serverCode = (int) session.getAttribute("code");
        if (!(serverCode + "").equals(code)) {
            //验证码不正确
            request.setAttribute("errinfo", "验证码不正确");
            return "error.jsp";
        }

        //验证码OK - 删除验证码
        session.removeAttribute("code");

        //修改密码
        int result = playerService.updatePassword(username, newpassword);
        if (result > 0) {
            //修改成功 跳转登录页
            return "redirect:login.jsp";
        }

        //修改失败
        request.setAttribute("errinfo", "找回密码失败！");
        return "error.jsp";
    }

    /**
     * 退出登录
     * @return
     */
    public String logout(HttpSession session){
        session.removeAttribute("login_user");
        return "redirect:login.jsp";
    }
}
